package service

import (
	"bytes"
	"fmt"
	"ksd-social-api/commons/global"
	"ksd-social-api/modules/common/oss/vo"
	"ksd-social-api/utils"
	"ksd-social-api/utils/adr"
	"mime/multipart"
	"path"
	"strings"
)

type OssUplaodService struct {
}

// 参考官网封装：https://help.aliyun.com/zh/oss/developer-reference/delete-buckets?spm=a2c4g.11186623.0.0.3afe780foO3cAv

/**
 * @desc 判断bucketname是否存在,存在返回true.反之false
 * @author feige
 * @date 2023-11-09
 * @version 1.0
 */
func (service *OssUplaodService) IsExistBucket(bucketName string) (bool, error) {
	// 创建存储空间。
	return global.OssClient.IsBucketExist(bucketName)
}

/**
 * @desc 创建bucketname
 * @author feige
 * @date 2023-11-09
 * @version 1.0
 */
func (service *OssUplaodService) CreateBucket(bucketName string) (bool, error) {
	// 创建存储空间。
	err := global.OssClient.CreateBucket(bucketName)
	if err != nil {
		return false, err
	}
	return true, nil
}

/**
 * @desc 删除桶
 * @author feige
 * @date 2023-11-09
 * @version 1.0
 */
func (service *OssUplaodService) delBucket(bucketName string) (bool, error) {
	// 创建存储空间。
	err := global.OssClient.DeleteBucket(bucketName)
	if err != nil {
		return false, err
	}
	return true, nil
}

/**
 * @desc 删除某个桶下面的文件信息
 * @author feige
 * @date 2023-11-09
 * @version 1.0
 */
func (service *OssUplaodService) DelObject(bucketName string, path string) (bool, error) {
	// 创建存储空间。
	// 获取bucket对象
	bucket, err := global.OssClient.Bucket(bucketName)
	err = bucket.DeleteObject(path)
	if err != nil {
		return false, err
	}
	return true, nil
}

/**
 * @desc beego融合oss实现文件上传功能
 * @author feige
 * @date 2023-11-09
 * @version 1.0
 */
func (service *OssUplaodService) UploadOssFile(
	bucketName string,
	modelPath string,
	userUuid string,
	oldname string,
	file multipart.File, header *multipart.FileHeader) *vo.OssFileVo {
	// 判断当前传递的bucketname是否在oss服务器上存在
	isBucket, _ := service.IsExistBucket(bucketName)
	// 如果不存在
	if !isBucket {
		// 就创建
		service.CreateBucket(bucketName)
	}
	// 获取bucket对象
	bucket, err := global.OssClient.Bucket(bucketName)
	if err != nil {
		return nil
	}
	// 更名防止重复
	var uuid string
	if len(oldname) > 0 {
		split := strings.Split(path.Base(oldname), ".")
		uuid = split[0]
	} else {
		uuid = utils.GetUUID()
	}

	fileName := path.Base(header.Filename)
	filesuffix := path.Ext(header.Filename)
	fileSize := header.Size

	newFileName := uuid + filesuffix
	// 定义业务隔离目录比如：文章artciles /课程coruse /头像avatar
	//YMDPath := time.Now().Format("2006/01/02")
	// 这里为什么要加密：说白就是不要把用户敏感信息暴露给外部用户得知
	md5Path := adr.Md5(userUuid)
	filePath := modelPath + "/" + md5Path
	// 使用putobject完成目录的创建 --------------动态创建目录的方法
	err = bucket.PutObject(filePath+"/", bytes.NewReader([]byte("")))
	// 开始上传文件-------------------这里就是文件上传到oss服务器的关键代码
	err = bucket.PutObject(filePath+"/"+newFileName, file)
	if err == nil {
		// 开始返回
		ossFileVo := vo.OssFileVo{}
		ossFileVo.Ext = filesuffix
		ossFileVo.FileName = fileName
		ossFileVo.FileSize = fileSize
		ossFileVo.NewFileName = fileName
		ossFileVo.Success = "1"
		ossFileVo.Url = fmt.Sprintf("https://%s.oss-cn-shenzhen.aliyuncs.com/%s/%s", bucketName, filePath, newFileName)
		ossFileVo.Path = filePath + newFileName
		// 这三个参数必须合法，要么前端传递过来
		ossFileVo.FileType = "image/png"
		ossFileVo.Width = "0"
		ossFileVo.Height = "0"
		return &ossFileVo
	}

	return nil
}
